<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <th:block th:include="include :: header('请求列表')" />
</head>
<body class="gray-bg">
<div class="container-div">
    <div class="row">
        <div class="col-sm-12 search-collapse">
            <form id="formId">
                <div class="select-list">
                    <ul>
                        <li>
                            <label>名称：</label>
                            <input type="text" name="reqName"/>
                        </li>
                        <li>
                            <a class="btn btn-primary btn-rounded btn-sm" onclick="$.table.search()"><i class="fa fa-search"></i>&nbsp;搜索</a>
                            <a class="btn btn-warning btn-rounded btn-sm" onclick="$.form.reset()"><i class="fa fa-refresh"></i>&nbsp;重置</a>
                        </li>
                    </ul>
                </div>
            </form>
        </div>

        <div class="btn-group-sm" id="toolbar" role="group">
            <a class="btn btn-danger multiple disabled" onclick="$.operate.removeAll()" shiro:hasPermission="system:request:remove">
                <i class="fa fa-remove"></i> 删除
            </a>
        </div>
        <div class="col-sm-12 select-table table-striped">
            <table id="bootstrap-table"></table>
        </div>
    </div>
</div>
<th:block th:include="include :: footer" />
<script th:inline="javascript">
    var editFlag = [[${@permission.hasPermi('system:request:edit')}]];
    var removeFlag = [[${@permission.hasPermi('system:request:remove')}]];
    var requestStatusDatas = [[${@dict.getType('sys_request_status')}]];
    var prefix = ctx + "system/request2";

    $(function() {
        var options = {
            url: prefix + "/history/list",
            removeUrl: prefix + "/history/remove",
            modalName: "请求",
            sortName: "createTime",
            sortOrder: "desc",
            onLoadSuccess: function (data){
                onLoadSuccess(data);
            },
            columns: [{
                checkbox: true
            },
                {
                    field: 'reqId',
                    title: '主键',
                    visible: false
                },
                {
                    field: 'reqName',
                    title: '名称'
                },
                {
                    field: 'reqStatus',
                    title: '状态',
                    formatter: function(value, row, index) {
                        return $.table.selectDictLabel(requestStatusDatas, value);
                    }
                },
                {
                    field: 'samples',
                    title: '样品',
                    formatter: function (value, item, index) {
                        var samples = [];
                        $.each(value,function (index, data) {
                            var s = JSON.stringify(data);
                            var parse = JSON.parse(s);
                            if(parse.priority == 1){
                                //优先
                                samples.push('<a class="sam-'+parse.samId +'" href="javascript:;" onclick="$.operate.view2(\''+parse.samId+'\',\'样品\')"><i class="fa fa-flask"></i> '+parse.samName+' <i class="fa fa-flash" style="color: brown"></i></ac><br>')
                            }else {
                                samples.push('<a class="sam-'+parse.samId +'" href="javascript:;" onclick="$.operate.view2(\''+parse.samId+'\',\'样品\')"><i class="fa fa-flask"></i> '+parse.samName+'</a><br>')
                            }
                        })
                        return samples.join('');
                    }
                },
                {
                    field: 'services',
                    title: '服务',
                    formatter: function (value, item, index) {
                        var services = [];
                        $.each(value,function (index, data) {
                            var s = JSON.stringify(data);
                            var parse = JSON.parse(s);
                            services.push('<a class="sam-'+parse.sample.samId +'" id="serv'+parse.infId+'" href="javascript:;" onclick="$.operate.view3(\''+parse.infId+'\',\'服务\')"><i class="fa fa-adjust fa-lg"></i> '+parse.serName+'</a><br>')
                        })
                        return services.join('');
                    }
                },
                {
                    field: 'reqDes',
                    title: '描述'
                },
                {
                    field: 'project.proName',
                    title: 'Project'
                },
                {
                    field: 'createTime',
                    title: '创建时间',
                    sortable: true
                },
                {
                    field: 'comment',
                    title: '添加意见',
                    visible: false
                },
                {
                    title: '操作',
                    // align: 'center',
                    formatter: function(value, row, index) {
                        var actions = [];
                        var services = '<a class="btn btn-info btn-xs ' + editFlag + '" href="javascript:void(0)" onclick="detail(\'' + row.reqId + '\')"><i class="fa fa-list"></i> 服务</a> ';
                        var remove = '<a class="btn btn-danger btn-xs ' + removeFlag + '" href="javascript:void(0)" onclick="$.operate.remove(\'' + row.reqId + '\')"><i class="fa fa-remove"></i> 删除</a>';
                        actions.push(services);
                        actions.push(remove);
                        return actions.join('');
                    }
                }]
        };
        $.table.init(options);
    });

    function onLoadSuccess(data){
        $.each(data.rows, function (index, value){
            if(value.length == 0){
                return;
            }
            var samIds = [];
            $.each(value.services, function( key, value, index) {
                $("#serv" + value.infId +" i:first-child").css("color","green");
                $("#serv" + value.infId +" i:first-child").removeClass();
                $("#serv" + value.infId +" i:first-child").addClass("fa fa-check fa-lg")
                if(!samIds.includes(value.sample.samId)){
                    samIds.push(value.sample.samId);
                }
            });

            //随机颜色
            var clolrs = ["green", "blue", "red", "darkslategray", "olive", "purple", "navy", "darkorange"];
            for (let i = 0; i < samIds.length; i++) {
                $(".sam-"+samIds[i]).css("color", clolrs[i%8]);
            }
        });
    }

    /*服务列表-详细*/
    function detail(reqId) {
        var url = prefix + '/services/' + reqId;
        $.modal.openTab("服务列表", url);
    }


</script>
</body>
</html>